package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.pojo.TbAddress;
import com.guigu.qingcheng.pojo.TbOrder;
import com.guigu.qingcheng.service.CartService;
import com.guigu.qingcheng.service.OrderServiceFeign;
import com.guigu.qingcheng.service.UserServiceFeign;
import com.guigu.qingcheng.utils.JwtUtils;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private UserServiceFeign userServiceFeign;

    @Autowired
    private OrderServiceFeign orderServiceFeign;


    // 获取购物车列表
    @GetMapping("/findCartList")
    @ResponseBody
    public List<Map<String, Object>> findCartList( HttpServletRequest request){
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String username = JwtUtils.getMemberIdByJwtToken(request);
        List<Map<String, Object>> cartList = cartService.findCartList(username);
        return cartList;
    }

    // 添加购物车 修改 删除
    @GetMapping("/addItem")
    @ResponseBody
    public R addItem(String skuId,Integer num, HttpServletRequest request){
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String username = JwtUtils.getMemberIdByJwtToken(request);
        cartService.addItem(username,skuId,num);
        return R.ok();
    }

    // 转购物车页面
    @RequestMapping("/cartUI")
    public String registerUI() {
        return "cart";
    }

    // 商品详细页调用 添加购物车
    @GetMapping("/buy")
    public String buy(String skuId,Integer num, HttpServletRequest request) throws IOException {
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String username = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println("username===="+username);
        cartService.addItem(username,skuId,num);
        return "forward:/cartUI";
    }

    @GetMapping("/updateChecked")
    @ResponseBody
    public R updateChecked(String skuId, boolean checked, HttpServletRequest request){
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String username = JwtUtils.getMemberIdByJwtToken(request);
        boolean b = cartService.updateChecked(username, skuId, checked);

            return R.ok();
    }


    /**
     * 删除选中的购物车
     * @param
     */
    @GetMapping("/deleteCheckedCart")
    @ResponseBody
    public void deleteCheckedCart( HttpServletRequest request){
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String username = JwtUtils.getMemberIdByJwtToken(request);
        cartService.deleteCheckedCart(username);
    }

    /**
     * 计算当前购物车优惠金额
     * @param
     * @return
     */
    @GetMapping("/preferential")
    @ResponseBody
    public Map preferential( HttpServletRequest request){
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
       String username = JwtUtils.getMemberIdByJwtToken(request);
        int preferential = cartService.preferential(username);
        Map map=new HashMap();
        map.put("preferential",preferential);
        return map;
    }

    @GetMapping("/findNewOrderItemList")
    @ResponseBody
    public List<Map<String,Object>> findNewOrderItemList( HttpServletRequest request){
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
       String username = JwtUtils.getMemberIdByJwtToken(request);
        List<Map<String, Object>> newCartList = cartService.findNewCartList(username);
        return newCartList;
    }


    // 转结算页面
    @RequestMapping("/jieSuanUI")
    public String jieSuanUI() {
        return "order";
    }

    @GetMapping("/findAddressList")
    @ResponseBody
    public List<TbAddress> findAddressList(HttpServletRequest request){
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
       String username = JwtUtils.getMemberIdByJwtToken(request);
        List<TbAddress> addressList = userServiceFeign.findAddressList(username);

        return addressList;
    }

    @PostMapping("/findNewOrderItemList2")
    @ResponseBody
    public List<Map<String,Object>> findNewOrderItemList2(@RequestBody String username){
        List<Map<String, Object>> newCartList = cartService.findNewCartList(username);
        return newCartList;
    }

    /**
     * 计算当前购物车优惠金额2
     * @param
     * @return
     */
    @PostMapping("/preferential2")
    @ResponseBody
    public int preferential2(@RequestBody String username){
        int preferential = cartService.preferential(username);
        return preferential;
    }

    /**
     * 删除选中的购物车
     * @param
     */
    @PostMapping("/deleteCheckedCart2")
    @ResponseBody
    public void deleteCheckedCart2(@RequestBody String username){
        cartService.deleteCheckedCart(username);
    }


    /**
     * 添加订单
     * @param
     */
    @PostMapping("/addOrder")
    @ResponseBody
    public Map<String,Object> addOrder(@RequestBody TbOrder tbOrder, HttpServletRequest request){
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String username = JwtUtils.getMemberIdByJwtToken(request);
        tbOrder.setUsername(username);
        Map<String, Object> stringObjectMap = orderServiceFeign.addOrder(tbOrder);
        return stringObjectMap;
    }



    // 转在线订单页面
    @RequestMapping("/pay")
    public String pay(String ordersn, int money, Model model) {
        model.addAttribute("ordersn",ordersn);
        model.addAttribute("money",money);
        return "pay";
    }

    // 转货到付款页面
    @RequestMapping("/huoDaoFukPay")
    public String huoDaoFukPay(String ordersn, int money, Model model) {
        model.addAttribute("ordersn",ordersn);
        model.addAttribute("money",money);
        return "order-success";
    }
    // 转失败页面
    @RequestMapping("/orderFailUI")
    public String orderFailUI() {
        return "order-fail";
    }


}
